<script setup lang="ts">
import {Client_PATCH, Super_GET} from "~/utils/httpUtils";

definePageMeta({
  //布局
  layout: 'landing-layout',
  validate: async (route: any) => {
    return true
  }
})
onMounted(() => {
  handleAdminGetUserList()
})
interface Amount {
  total: number;
  payer_total?: number;
  currency?: string;
  payer_currency?: string;
}
interface Payer {
  openid: string;
  socketid?: string;
}
interface PayOrder {
  _id: string;
  user_id: string; //user_id,用户id
  out_trade_no: string; //订单号
  description: string;
  channel: string; //支付渠道
  amount: Amount; //金额
  order_status: 0 | 1 | 2; //0:未支付 1:已支付 2:已退款;
  order_create_time: number;
  order_pay_time: number;
  transaction_id: string;
  trade_type: string;
  payer: Payer;  //支付者
}
type PayOrderKeys = keyof PayOrder;
const tableHeaders:PayOrderKeys[] = ['_id','user_id','out_trade_no','description','amount','order_status','order_create_time','order_pay_time','payer']
const tableData = ref<PayOrder[]>([])

const handleAdminGetUserList = async () => {
  tableData.value = await Client_GET('/pay/orders') as PayOrder[]
}
// const handleAdminUpdateUserStatus = async (uid: string, status: 0 | 1|2) => {
//   //如果是管理员账户，不允许操作
//   if (tableData.value.find(item => item._id === uid)?.username.includes('admin')) {
//     useSnackbarStore().showErrorMessage('管理员账户不允许操作')
//     return
//   }
//   await Client_PATCH(`/users/${uid}`,{status})
//   useSnackbarStore().showSuccessMessage(`${status === 0 ? '启用' : '禁用'}成功`)
//   //同步更新本地表格数据
//   tableData.value = tableData.value.map(item => {
//     if (item._id === uid) {
//       item.status = status
//     }
//     return item
//   })
// }

</script>

<template>
  <v-table
      fixed-header
  >
    <thead>
    <tr>
      <th v-for="header in tableHeaders" class="text-left">
        {{header.toUpperCase()}}
      </th>
    </tr>
    </thead>
    <tbody>
    <tr v-for="item in tableData">
      <td v-for="key in tableHeaders">
        <span v-if="key === 'order_create_time'||key === 'order_pay_time'">
          {{new Date(item[key]).toLocaleString()}}
        </span>
        <!--        显示用户状态 0 正常 1 禁用 3 删除-->
        <span v-else-if="key === 'order_status'" class="d-flex">
          <v-chip size="x-small" :color="item[key] === 1 ? 'green' : 'red'">
            {{item[key] === 1 ? '已支付' : '未支付'}}
          </v-chip>
<!--          <v-btn @click="handleAdminUpdateUserStatus(user._id,user[key] === 0 ? 1 : 0)" size="x-small" :color="user[key] === 0 ? 'warning' : 'green'">-->
<!--            {{user[key] === 0 ? '禁用' : '启用'}}-->
<!--          </v-btn>-->
        </span>
        <span v-else-if="key === 'amount'">
          {{item[key]?.total/100}}
        </span>
        <span v-else-if="key === 'payer'">
          {{item[key]?.openid}}
        </span>
        <span v-else>
          {{item[key]}}
        </span>
      </td>
    </tr>
    </tbody>
  </v-table>
</template>

<style scoped lang="scss">

</style>